package com.luboyan.CBillbill.controller;

import com.alibaba.fastjson.JSONObject;
import com.luboyan.CBillbill.dao.file.Video;
import com.luboyan.CBillbill.advice.TokenVerify;
import com.luboyan.CBillbill.dao.user.User;
import com.luboyan.CBillbill.dao.user.UserInfo;
import com.luboyan.CBillbill.dao.vo.PageVo;
import com.luboyan.CBillbill.service.user.UserService;
import com.luboyan.CBillbill.util.Result;
import com.luboyan.CBillbill.util.encrypt.RSAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户信息管理
 */

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenVerify tokenVerify;

    /**
     * 获取公钥发送给前端加密数据（RSA算法）
     */
    @GetMapping("/rsa-pks")
    public Result<String> getRsaPublicKey() {
        String publicKey = RSAUtil.getPublicKeyStr();
        return new Result<>(publicKey);
    }

    /**
     * 添加用户
     */
    @PostMapping
    public Result<String> addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success();
    }

//    /**
//     * 登录
//     * @return 返回一个token给前端
//     */
//    @PostMapping("/login")
//    public Result<String> login(@RequestBody User user) {
//        String token = userService.login(user);
//        return new Result<>(token);
//    }

    /**
     * 双token登录机制
     */
    @PostMapping("/login")
    public Result<Map<String, Object>> dLogin(@RequestBody User user) {
        Map<String, Object> result = userService.dLogin(user);
        return new Result<>(result);
    }

    /**
     * 退出登录
     */
    @DeleteMapping("/layout")
    public Result<String> logout(HttpServletRequest request) {
        String refreshToken = request.getHeader("refreshToken");
        Long userId = tokenVerify.getCurrentUserId();
        userService.logout(refreshToken);
        return Result.success();
    }

    /**
     * 刷新token
     */
    @PostMapping("/refreshToken")
    public Result<String> refreshToken(HttpServletRequest request) {
        String refreshToken = request.getHeader("refreshToken");
        String token = userService.refreshToken(refreshToken);
        return new Result<>(token);
    }


    /**
     * 获取用户的账号信息
     */
    @GetMapping
    public Result<User> getUserInfo() {
        Long userId = tokenVerify.getCurrentUserId();
        User user = userService.getUserInfo(userId);
        return new Result<>(user);
    }

    /**
     * 修改用户账号信息
     */
    @PutMapping
    public Result<String> updateUser(@RequestBody User user) throws Exception {
        Long userId = tokenVerify.getCurrentUserId();
        user.setId(userId);
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 更新用户的基本信息
     */
    @PutMapping("/infos")
    public Result<String> updateUserInfo(@RequestBody UserInfo userInfo) {
        Long userId = tokenVerify.getCurrentUserId();
//        userInfo.setId(userId);
        userInfo.setUserId(userId);
        userService.updateUserInfo(userInfo);
        return Result.success();
    }

    /**
     * 分页获取用户信息，用于在搜索后展示，还可以顺便判断是否被当前用户关注
     */
    @GetMapping("/infos")
    public Result<PageVo<UserInfo>> getPageUserInfos(@RequestParam Integer page, @RequestParam Integer pageSize, String nick) {
        Long userId = tokenVerify.getCurrentUserId();
        JSONObject params = new JSONObject();  //代替map使用
        params.put("page", page);
        params.put("pageSize", pageSize);
        params.put("nick", nick);
        params.put("userId", userId);
        PageVo<UserInfo> pageUserInfos = userService.getPageUserInfos(params);
        return new Result<>(pageUserInfos);
    }

    /**
     * 返回用户的视频
     */
    @GetMapping("/videos")
    public Result<PageVo<Video>> getVideosByUserId(Integer page, Integer pageSize) {
        Long userId = tokenVerify.getCurrentUserId();
        PageVo<Video> result = userService.getVideosByUserId(page, pageSize, userId);
        return new Result<>(result);
    }

//    /**
//     * 用户给自己视频添加标签
//     */
//    @PostMapping("/videos")
//    public Result<String> addVideoTags(){
//
//    }



}
